﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class MailHelper
    {
        // Methods
        public static void SendAsyncEmail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, bool isHtmlFormat, string[] files, object userToken, SendCompletedEventHandler onComplete)
        {
            try
            {
                SmtpClient client = new SmtpClient(strSmtpServer)
                {
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(strFrom, strFromPass),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };
                MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody)
                {
                    BodyEncoding = Encoding.UTF8,
                    IsBodyHtml = isHtmlFormat
                };
                if (files != null)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (File.Exists(files[i]))
                        {
                            message.Attachments.Add(new Attachment(files[i]));
                        }
                    }
                }
                client.SendCompleted += new SendCompletedEventHandler(onComplete.Invoke);
                client.SendAsync(message, userToken);
            }
            catch (Exception exception)
            {
                throw new Exception("发送邮件失败。错误信息：" + exception.Message);
            }
        }

        public static void SendEmail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, bool isHtmlFormat)
        {
            SendEmail(strSmtpServer, strFrom, strFromPass, strto, strSubject, strBody, isHtmlFormat, null);
        }

        public static void SendEmail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, bool isHtmlFormat, string[] files)
        {
            try
            {
                SmtpClient client = new SmtpClient(strSmtpServer)
                {
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(strFrom, strFromPass),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };
                MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody)
                {
                    BodyEncoding = Encoding.UTF8,
                    IsBodyHtml = isHtmlFormat
                };
                if (files != null)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        if (File.Exists(files[i]))
                        {
                            message.Attachments.Add(new Attachment(files[i]));
                        }
                    }
                }
                client.Send(message);
            }
            catch (Exception)
            {
            }
        }
    }

}
